package yl.hs.bmipfull.controllers.flow;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.flow.IFlowInfoService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.flow.FlowInfoViewModel;

/**
 * 作用:  流程信息控制层<br/>
 * 创建时间: 2021年06月22日 14时30分49秒 <br/>
 *
 * @author 张凯
 */
@Api(value = "流程信息Web接口", tags = "流程管理——流程信息操作")
@RequestMapping(value = "/api/flowInfo")
@RestController
public class FlowInfoController extends ControllerBase {

    @Autowired
    IFlowInfoService service;

    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "修改流程数据表信息状态", notes = "根据流程数据表状态修改流程数据表信息状态")
    @RequestMapping(path = "/updateState/{state}", method = RequestMethod.POST)
    public WebResult updateState(@ApiParam(value = "页码") @PathVariable int state) {
        short whereState = 0;
        if (state == 0) {
            whereState = 1;
        } else if (state == 1) {
            whereState = 0;
        } else {
            return fail("修改状态值有误！");
        }
        if (service.updateStateByState((short) state, whereState) > 0) {
            return success("修改成功");
        } else {
            return fail("修改失败");
        }
    }

    @ApiOperation(value = "查询流程信息", notes = "查询流程信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询流程信息", notes = "分页查询流程信息")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "验证数据类型编码是否已经存在", notes = "验证数据类型编码是否已经存在")
    @RequestMapping(value = "/valida", method = RequestMethod.GET)
    public WebResult checkDataTypeCode(@ApiParam(value = "数据类型代码") @RequestParam String dataTypeCode,
                                       @ApiParam(value = "数据编号") @RequestParam String sn) {
        if (service.checkDataTypeCode(dataTypeCode, sn)) {
            return success("已经存在", true);
        } else {
            return success("该类型不存在", false);
        }

    }


    @ApiOperation(value = "查询流程信息", notes = "根据流程信息编号查询流程信息")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存流程信息", notes = "查询/更新流程信息信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveFlowInfo(@ApiParam(value = "提交信息") @RequestBody FlowInfoViewModel flowInfoViewModel) {
        Integer result;
        try {
            result = service.saveFlowInfo(flowInfoViewModel);
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "保存流程信息", notes = "查询/更新流程信息信息")
    @RequestMapping(value = "/base", method = RequestMethod.PUT)
    public WebResult saveFlowInfo(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveFlowInfo(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }


    @ApiOperation(value = "删除流程信息信息", notes = "根据流程信息编号删除系流程信息信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "流程信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }

    }
}